<template>
  <a-drawer
    :title="modelTitle + '权限配置'"
    placement="right"
    :visible="visible"
    :keyboard="false"
    :width="600"
    @close="deawerClosed"
  >
    <a-spin :spinning="spinning">
      <div class="drawer_main">
        <a-tree
          v-if="!spinning"
          autoExpandParent
          v-model="checkedKeys"
          checkable
          defaultExpandAll
          :treeData="treeList"
          @check="onCheck"
          :replaceFields="replaceFields"
        />
      </div>
    </a-spin>
    <div class="footer">
      <a-button @click="confirmHandler" type="primary">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
import { getMenuList } from '@/api/system'

export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  model: {
    prop: 'visible',
    event: 'closeHandler'
  },
  data() {
    return {
      modelTitle: '',
      spinning: true,
      confirmLoading: false,
      form: {},
      treeList: [],
      replaceFields: { children: 'children', title: 'name', key: 'menuId' },
      expandedKeys: [],
      selectedKeys: [],
      checkedKeys: [],
      checkedParentKeys: [],
      menuIds: []
    }
  },
  mounted() {},
  methods: {
    initForm(id, name) {
      this.spinning = id > 0
      this.modelTitle = name
      this.getTreeList(() => {
        // 控制是否加载loading
        if (id) {
          this.spinning = false
          const ids = [117, 170, 142, 144, 168, 152, 153, 151]
          this.form.ids = ids
          this.checkedKeys = this.initMenuIds(ids)
        }
        this.showModel = true
      })
    },
    confirmHandler() {
      console.log(this.form.ids)
    },
    getTreeList(callback) {
      getMenuList().then(res => {
        this.treeList = res.data
        this.spinning = false
        callback && typeof callback === 'function' && callback()
      })
    },
    onCheck(checkedKeys, a) {
      console.log('onCheck', checkedKeys, a)
      this.checkedKeys = checkedKeys
      this.checkedParentKeys = a.halfCheckedKeys
      this.form.ids = [...this.checkedKeys, ...this.checkedParentKeys]
    },
    handleChange(value) {
      console.log(`selected ${value}`)
    },
    deawerClosed() {
      this.$emit('closeHandler', false)
      this.form = {}
    },
    // 初始化菜单选中 id
    initMenuIds(roleIds) {
      const preRoleIds = []
      function hasChildren(preItem) {
        if (!Array.isArray(preItem.children) || !preItem.children.length) return false
        if (!preRoleIds.includes(preItem.menuId)) preRoleIds.push(preItem.menuId)
        preItem.children.forEach(childItem => {
          hasChildren(childItem)
        })
      }
      this.treeList.forEach(item => {
        hasChildren(item)
      })
      return roleIds.filter(i => {
        return !preRoleIds.includes(i)
      })
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.ant-drawer-wrapper-body {
  display: flex;
  flex-direction: column;
  position: relative;

  .ant-drawer-body {
    flex: 1;

    .ant-spin-nested-loading {
      height: 100%;
    }

    .ant-spin-container {
      height: 100%;
    }
  }
}
.drawer_main {
  width: 100%;
  height: 100%;
  padding-bottom: 50px;
}
.footer {
  position: absolute;
  bottom: 0;
  right: 0;
  display: flex;
  flex-direction: row-reverse;
  width: 100%;
  padding: 10px 20px;
  border-top: 1px solid #999999;
}
</style>
